/**
 * <strong>Title : WorkUnitTypeServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.service.impl<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年9月24日 上午11:25:36<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.cm.dao.WorkUnitDao;
import cn.b.sky.mes.cm.dao.WorkUnitTypeDao;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.WorkUnitType;
import cn.b.sky.mes.cm.model.WorkUnitTypeAssign;
import cn.b.sky.mes.cm.model.condition.WorkUnitTypeCondition;
import cn.b.sky.mes.cm.service.WorkUnitTypeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * <strong>Title : WorkUnitTypeServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.service.impl<br></strong>
 * <strong>Description : </strong>@工作单元类型服务类实现@<br> 
 * <strong>Create on : 2015年9月24日 上午11:25:36<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("workUnitTypeService")
public class WorkUnitTypeServiceImpl implements WorkUnitTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(WorkUnitTypeServiceImpl.class);

  @Resource
  WorkUnitTypeDao workUnitTypeDao;

  @Resource
  WorkUnitDao workUnitDao;

  @Override
  public List<WorkUnitType> selectWorkUnitTypeList(WorkUnitTypeCondition workUnitTypeCondition) {
    if (StringUtils.isBlank(workUnitTypeCondition.getOrderField())) {
      workUnitTypeCondition.setOrderField("CreateDateTime");
    }
    return workUnitTypeDao.selectWorkUnitTypeList(workUnitTypeCondition);
  }

  @Override
  public int getWorkUnitTypeCount(WorkUnitTypeCondition workUnitTypeCondition) {
    return workUnitTypeDao.getWorkUnitTypeCount(workUnitTypeCondition);
  }

  @Override
  public WorkUnitType getWorkUnitType(String id, String siteCode) {
    WorkUnitTypeCondition condition = new WorkUnitTypeCondition();
    condition.setSiteCode(siteCode);
    condition.setWorkUnitTypeCode(id);
    return workUnitTypeDao.getWorkUnitType(condition);
  }

  @Override
  public void insertWorkUnitType(WorkUnitType workUnitType) {
    workUnitTypeDao.insertWorkUnitType(workUnitType);

  }

  @Override
  public void updateWorkUnitType(WorkUnitType workUnitType) {
    workUnitTypeDao.updateWorkUnitType(workUnitType);

  }

  @Override
  public BatchJobReport deleteWorkUnitTypes(String workUnitTypeCode, String siteCode) {
    if (!StringUtils.isBlank(workUnitTypeCode)) {
      String message = workUnitTypeCode;
      String cause = "";
      boolean success = true;
      try {
        workUnitTypeDao.deleteWorkUnitTypes(workUnitTypeCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
              "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    } else {
      return new BatchJobReport("", "id is null", false, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    }

  }

  @Override
  public int getWorkUnitTypeExactCount(WorkUnitTypeCondition workUnitTypeCondition) {
    return workUnitTypeDao.getWorkUnitTypeExactCount(workUnitTypeCondition);
  }

  @Override
  public void insertWorkUnitTypeAssign(WorkUnitType workUnitType, String[] workUnitIds, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitTypeCode(workUnitType.getWorkUnitTypeCode());
    for (String workUnitId : workUnitIds) {
      assign.setWorkUnitCode(workUnitId);
      workUnitTypeDao.insertWorkUnitTypeAssign(assign);
    }

  }

  @Override
  public List<WorkUnit> selectAssignByUnitType(String id, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitTypeCode(id);
    return workUnitTypeDao.selectAssignByUnitType(assign);
  }

  @Override
  public void updateWorkUnitTypeAssign(WorkUnitType workUnitType, String[] workUnitIds, String siteCode) {
    WorkUnitTypeAssign workUnitTypeAssign = new WorkUnitTypeAssign();
    workUnitTypeAssign.setSiteCode(siteCode);
    workUnitTypeAssign.setWorkUnitTypeCode(workUnitType.getWorkUnitTypeCode());
    workUnitTypeDao.deleterAssignByUnitType(workUnitTypeAssign);

    insertWorkUnitTypeAssign(workUnitType, workUnitIds, siteCode);

  }

  @Override
  public WorkUnitType getWorkUnitType(WorkUnitTypeCondition workUnitTypeCondition, String siteCode) {
    workUnitTypeCondition.setSiteCode(siteCode);
    return workUnitTypeDao.getWorkUnitType(workUnitTypeCondition);
  }

  @Override
  public List<WorkUnitType> selectAssignByUnit(String id, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitCode(id);
    return workUnitTypeDao.selectAssignByUnit(assign);
  }

  @Override
  public void deleterAssignByUnit(WorkUnit workUnit, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitCode(workUnit.getWorkUnitCode());

    workUnitTypeDao.deleterAssignByUnitType(assign);
  }

  @Override
  public void deleterAssignByUnitType(WorkUnitType workUnitType, String siteCode) {
    WorkUnitTypeAssign assign = new WorkUnitTypeAssign();
    assign.setSiteCode(siteCode);
    assign.setWorkUnitTypeCode(workUnitType.getWorkUnitTypeCode());

    workUnitTypeDao.deleterAssignByUnitType(assign);
  }

  @Override
  public List<WorkUnitType> getUnlockWorkUnitTypes() {
    return null;
  }

  @Override
  public List<WorkUnitType> selectWorkUnitTypeSuggests(WorkUnitTypeCondition condition) {
    return workUnitTypeDao.selectWorkUnitTypeSuggests(condition);
  }

}
